/**
 * 用于页面级别的JS
 * wolfer
 * wolfers@126.com
 * 2013-04-10
 */

define(["js/mCard.js","js/mCommon.js","js/mVideo.js"],function(card,common,video){  
	var allRolesArr;
	var R = {
			/*创建列表*/
			create:function(data,id,hasCreapTime){
				rolesArr = data.data.userRoles;
				var lists = $("#roleLists");
				var li = [];
				
				
				for(var i=0; i<rolesArr.length; i++){
					
					if(!rolesArr[i].unLocked){
						var img = "logo_gay.png";
						var lockClass="lock";
						var gayClass="gay";
					}else{
						var img = "logo.png";
						var lockClass="";
						var gayClass="";
					}
					li.push("<li class='item' id='"+rolesArr[i].id+"' score='"+rolesArr[i].unlockPrice+"'  name='"+rolesArr[i].roleResrouceName+"'>" +
							"<img class='imgType "+gayClass+"' style='width:107px;height:107px' src='images/"+rolesArr[i].roleResrouceName+"/"+img+"'>" +
							"<span class='"+lockClass+" icon type'></span>"+
							"</li>");
				}
				lists.html(li.join(""));
				
				
				if(hasCreapTime){
					var roleId = data.data.usedRoleId;
					this.isCreapTime(id,roleId);
				}
				
				
			},
			
		setLists:function(id,hasCreapTime){
			var  _t = this;
			var url = Game.getBaseURL() + "/role/"+id;
			console.log(url);
			$.ajax({
				url: url,
				dataType:"json",
				success: function(data){
					if(data.success){
						_t.create(data,id,hasCreapTime);
					}
				}
			});
		},	
		
		createUser:function(id,isNext){
			var  _t = this;
			var url = Game.getBaseURL() + "/popup/user?userId="+id;
			$.ajax({
				url: url,
				dataType:"json",
				success: function(data){
					if(data.success){
						var data = data.data.userInfo;
						$("#tangNum-all").attr("num",data.candy).html(card.getNumGif(data.candy,"blue"));
						$("#xingNum-all").attr("num",data.star).html(card.getNumGif(data.star,"blue"));
						if(isNext){
							_t.nextProps();
						}
					}
				}
			});
		},
		
		upgradeList:function(data,id){
			
			var score = data.unlockPrice;
			
			//userId, resId（角色资源ID）, nextLevel
			var resId = data.roleResourceId;
			var level = data.level;
			var roleId = data.id;
			var roleResrouceName = data.roleResrouceName;
			console.log("现在的等级是:"+level);
			$("#needScore").html(card.getNumGif(score,"blue"));
			$("#niceIcon").show().addClass("niceIcon-"+level);
			$("#roleId").val(roleId);
			$("#resId").val(resId);
			$("#level").val(level);
			$("#roleResrouceName").val(roleResrouceName);
			
			//请求下一等级的资源
			this.nextProps();
			
		},
		
		/*提升等级*/
		upgrade:function(id){
			var userId = id;
			var resId = $("#resId").val();
			var nextLevel =parseInt($("#level").val())+1;
			var url = Game.getBaseURL() + "/role/upgrade";
			var _t =this;
			$.ajax({
				url: url,
				dataType:"json",
				type: "POST",
				data: "userId="+userId+"&resId="+resId+"&nextLevel="+nextLevel,
				success: function(data){
					console.log("userId="+userId+"&resId="+resId+"&nextLevel="+nextLevel);
					if(data.success){
						var niceImg = "<img  id='niceImg' class='niceImg' src='images/haogandu.gif'  />";
				    	$("#scene").append(niceImg);
				    	$("#niceIcon").addClass("niceIcon-"+nextLevel);
				    	setTimeout(function(){
				    		$("#niceImg").remove();
				    	},3000);
				    	_t.change($("#roleId").val());
				    	_t.createUser(id,true);
					}else{
						$().alert({content:data.message});
						
					}
				}
			});
		},
		
		
		change:function(roleId){
			console.log("切换用户");
			var _t =this;
			var url = Game.getBaseURL() + "/role/change?r="+roleId;
			$.ajax({
				url: url,
				dataType:"json",
				success: function(data){
					if(data.success){
						console.log("切换用户成功");
					}else{
						console.log("切换用户失败");
						$().alert({content:data.message});
					}
				}
			});
		},
		
		
		
		/*角色解锁*/
		unlock:function(){
			var userId = dataStorage.get(MyOwner.userId);
			var roleId = $("#roleId").val();
			console.log(userId+":"+roleId)
			var url = Game.getBaseURL() + "/role/unlock";
			$.ajax({
				url: url,
				type: "POST",
				data: "userId="+userId+"&roleId="+roleId,
				dataType:"json",
				success: function(data){
					//data = JSON.parse(data);
					
					if(data.success){
						$().alert({content:data.message});
						/*角色解锁成功  更换角色状态*/
						var roleResrouceName = $("#roleResrouceName").val();
						$("#bigImg").attr("src","images/"+roleResrouceName+"/create_big.png");
						$(".picScroll .itemOn>img").attr("src","images/"+roleResrouceName+"/logo.png");
						$(".picScroll .itemOn>img").removeClass("gay");
						$(".picScroll .itemOn .type").removeClass("lock");
					}else{
						$().alert({content:data.message});
					}
					
				}
			});
		},
		
		/*是否可以获取*/
		isCreapTime:function(userId,roleId){
			///isReapTime/{userId}/{roleId}
			var url = Game.getBaseURL() + "/role/isReapTime/"+userId+"/"+roleId;
			var _t = this;
			$.ajax({
				url: url,
				dataType:"json",
				success: function(data){
					if(data.success && data.data.success){
						$("#niceButton").addClass("isCreapTime-button").removeClass("peopleHas-button");
						var niceButton = $(".isCreapTime-button");
						niceButton.live(common.getEvent[0],function(){
				    		$(this).addClass("on-120");
				    	});
						niceButton.live(common.getEvent[1],function(){
				    		$(this).removeClass("on-120");
				    		_t.reap();
				    	});
					}
				}
			});
		},
		
		reap:function(){
			var userId = dataStorage.get(MyOwner.userId);
			var roleId = $("#roleId").val();
			var url = Game.getBaseURL() + "/role/reap";
			$.ajax({
				url: url,
				type: "POST",
				data: "userId="+userId+"&roleId="+roleId,
				dataType:"json",
				success: function(data){
					//data = JSON.parse(data);
					if(data.success){
						$().alert({content:data.message});
					}else{
						$().alert({content:data.message});
					}
				}
			});
		},
		
		//role/{resId}/{level}
		nextProps:function(){
			var resId = $("#resId").val();
			var level =parseInt($("#level").val())+1;
			var url = Game.getBaseURL() + "/role/"+resId+"/"+level;
			var _t =this;
			$.ajax({
				url: url,
				dataType:"json",
				success: function(data){
					console.log(data);
					if(data.success){
						var star = data.data.role.star;
						var candy =  data.data.role.candy;
						$("#needStar").html(star);
						$("#needCandy").html(candy);
						
						var myCandy = parseInt($("#tangNum-all").attr("num"));
						var myStar = parseInt($("#xingNum-all").attr("num"));
						myCandy = myCandy>candy?candy : myCandy;
						myStar = myStar>star?star : myStar;
						$("#peopleHas-bg-star").width(myStar*(84/star));
						$("#peopleHas-bg-candy").width(myCandy*(84/candy));
					}else{
						
					}
				}
			});
		}
		
	}
	
	
	
	var Info = {
			setLists:function(id,hasCreapTime){
				var  _t = this;
				var url = Game.getBaseURL() + "/role/"+id;
				console.log(url);
				$.ajax({
					url: url,
					dataType:"json",
					success: function(data){
						if(data.success){
							_t.create(data);
						}
					}
				});
				
				this._event();
			},
			
			create:function(data){
				var _t = this;
				var rolesArr = data.data.userRoles;
				var lists = [];
				
				for(var i=0; i<rolesArr.length; i++){
					if(rolesArr[i].unLocked){
						var img = "create_big";
					}else{
						var img = "create_big_gay";
					}
					lists.push("<li class='item' data-id='"+rolesArr[i].id+"' score='"+rolesArr[i].unlockPrice+"' " +
							"data-measurements='"+rolesArr[i].bust+" "+rolesArr[i].waistline+" "+rolesArr[i].hipline+"' " +
							"data-name='"+rolesArr[i].name+"'" +
							"data-zodiac='"+rolesArr[i].zodiac+"'" +
							"data-interest='"+rolesArr[i].interest+"'" +
							"data-introduction='"+rolesArr[i].introduction+"'" +
							"data-unLocked='"+rolesArr[i].unLocked+"'" +
							"data-roleResrouceName='"+rolesArr[i].roleResrouceName+"'" +
							">"+
							"<img src='images/"+rolesArr[i].roleResrouceName+"/"+img+".png'>" +
							"</li>");
				}
				$("#roleLists").html(lists.join(""));
				
				
				$('#myRole .recommend').sliderImg({
					pageWith:360,
					leftMove:"#myRole .right-button",
					rightMove:"#myRole .left-button",
					onchange:function(t,n,settings){
						if(n===null){
							_t.changeRoleInfo(1);
						}else{
							_t.changeRoleInfo(n);
						}
					}
				});
				
				this.myInfo(data);
			},
			
			myInfo:function(data){
				var _t = this;
				var rolesArr = data.data.userRoles;
				var lists = [];
				for(var i=0; i<rolesArr.length; i++){
					if(rolesArr[i].unLocked){
						lists.push("<li class='item' data-id='"+rolesArr[i].id+"'   data-roleResrouceName='"+rolesArr[i].roleResrouceName+"'>"+
								"<img src='images/"+rolesArr[i].roleResrouceName+"/create_big.png'>" +
								"</li>");
					}
					
				}
				$("#myLists").html(lists.join(""));
				
				$('#myInfo .recommend').sliderImg({
					pageWith:360,
					leftMove:"#myInfo .right-button",
					rightMove:"#myInfo .left-button",
					onchange:function(t,n,settings){
						if(n===null){
							_t._Settings = settings;
							//_t.changeMyRole(2);
							console.log("--->"+_t.getMyRoleOrder());
							_t.setMyRolePost(_t.getMyRoleOrder());
						}else{
							_t.changeMyRole(n);
						}
					}
				});
			},
			
			changeRoleInfo:function(n){
				console.log("--->"+n);
				if(n-1<0){
					var m = $("#roleLists>li").length-1;
				}else{
					var m = n-1;
				}
				var lis = $("#roleLists>li").eq(m);
				var name = lis.attr("data-name");
				var measurements = lis.attr("data-measurements");
				var zodiac = lis.attr("data-zodiac");
				var interest = lis.attr("data-interest");
				var introduction = lis.attr("data-introduction");
				var roleResrouceName = lis.attr("data-roleResrouceName");
				var id = lis.attr("data-id");
				$("#data_name").html(name);
				$("#data_measurements").html(measurements);
				$("#data_zodiac").html(zodiac);
				$("#data_interest").html(interest);
				$("#data_introduction").html(introduction);
				
				var unLocked = lis.attr("data-unLocked");
				var score  = lis.attr("score");
				if(unLocked === "true"){
					$("#lockHead").show();
					$("#needScore").hide();
					$("#myRole-button").hide();
				}else{
					$("#lockHead").hide();
					$("#needScore").show();
					$("#needScoreNum").html(card.getNumGif(score,"blue"));
					$("#myRole-button").show().attr("data-id",id).attr("data-n",m).attr("data-roleResrouceName",roleResrouceName);
				}
				
			},
			
			_event:function(){
				var _t = this;
				$("#myRole-button").live("click",function(){
					var roleId = $(this).attr("data-id");
					var n = $(this).attr("data-n");
					var roleResrouceName = $(this).attr("data-roleResrouceName");
					_t.unlock(roleId,n,roleResrouceName);
				});
				
				//更改角色
				$("#myInfo-select").live("click",function(){
					if($("#myInfo-selectButton").hasClass("select-off")){
						var roleId = $(this).attr("roleId");
						var resrouceName = $(this).attr("data-roleResrouceName");
						_t.change(roleId,resrouceName);
					}
					
				});
			},
			
			/*角色解锁*/
			unlock:function(roleId,n,roleResrouceName){
				var _t = this;
				var userId = dataStorage.get(MyOwner.userId);
				var url = Game.getBaseURL() + "/role/unlock";
				$.ajax({
					url: url,
					type: "POST",
					data: "userId="+userId+"&roleId="+roleId,
					dataType:"json",
					success: function(data){
						//data = JSON.parse(data);
						if(data.success){
							$().alert({content:data.message});
							
							_t.lockRoleInfo(n,roleResrouceName);
						}else{
							$().alert({content:data.message});
						}
						
					}
				});
			},
			
			/*角色解锁成功  更换角色状态*/
			lockRoleInfo:function(n,roleResrouceName){
				$("#lockHead").show();
				$("#needScore").hide();
				$("#myRole-button").hide();
				var lis = $("#roleLists>li").eq(n);
				lis.find("img").attr("src","images/"+roleResrouceName+"/create_big.png");
			},
			
			/*切换角色 更改角色*/
			changeMyRole:function(n){
				console.log("切换角色 更改角色--->"+n);
				//取得我现在正使用的角色来源
				var userRoleName = dataStorage.get(MyOwner.roleName);
				console.log("正使用的角色："+userRoleName);
				if(n-1<0){
					var m = $("#myLists>li").length-1;
				}else{
					var m = n-1;
				}
				
				var lis = $("#myLists>li").eq(m);
				var changeRoleName =  lis.attr("data-roleResrouceName");
				var roleId =  lis.attr("data-id");
				$("#myInfo-select").attr("roleId",roleId).attr("data-roleResrouceName",changeRoleName);
				console.log("更改的角色："+changeRoleName);
				if(userRoleName == changeRoleName){
					$("#myInfo-selectButton").removeClass("select-off").addClass("select-on");
				}else{
					$("#myInfo-selectButton").removeClass("select-on").addClass("select-off");
				}
			},
			
			/*得到我的角色排在列表的位置*/
			getMyRoleOrder:function(){
				var userRoleName = dataStorage.get(MyOwner.roleName);
				var lis = $("#myLists>li");
				for(var i=0; i<lis.length; i++){
					var changeRoleName =  lis.eq(i).attr("data-roleResrouceName");
					if(userRoleName == changeRoleName){
						return i;
					}
				}
				return -1;
			},
			
			setMyRolePost:function(n){
				var _t = this;
				var w = 360; 
				var item = $("#myLists .items");
				var length = item.eq(0).find(".item").length;
				var l = -n*w;
				
				l = l > 0 ? -(length*w- l) :  l;
			
				_t._Settings.data._items.css({
			          "-webkit-transform": "translate("+l+"px, 0px)"
			    });
				_t._Settings.data._trans = l;
			},
			
			change:function(roleId,resrouceName){
				console.log("切换用户");
				var _t =this;
				var url = Game.getBaseURL() + "/role/change?r="+roleId;
				$.ajax({
					url: url,
					dataType:"json",
					success: function(data){
						if(data.success){
							console.log("切换用户成功");
							dataStorage.set(MyOwner.roleName,resrouceName);
							window.location.reload();
							
						}else{
							console.log("切换用户失败");
							$().alert({content:data.message});
						}
					}
				});
			},
	}
	
	
	
	
	/*BOSS*/
	var Boss = {
			init:function(n){
			
				var _t = this;
				var uid = dataStorage.get(MyOwner.userId);
				var url = Game.getBaseURL() + "/boss/"+uid+"/list";
				$.ajax({
					url: url,
					dataType:"json",
					success: function(data){
						if(data.success){
							 _t.createBoss(data.data.bossList,n);
						}
					}
				});
			},
			
			createBoss:function(data,n){
				var lists = [];
				for(var i=0; i<data.length; i++){
					
					if(data[i].locked){
						var lockHtml = "<span class='boss_lock boss_icon'></span>";
						var cost = data[i].challengeCosting;
						/*默认的那个高亮*/
						if((n-1)===i){
							var img = "create_big.png";
						}else{
							var img = "create_big_gay.png";
						}
						
					}else{
						var lockHtml = "<span class='boss_new boss_icon'></span>";
						var cost = 1000;
						var img = "create_big.png";
					}
					lists.push("<div class='item'  free='"+data[i].freeChallengeCountRemain+"' cost='"+cost+"' roomId='"+data[i].roomId+"'>"+lockHtml+"<img   src='images/"+data[i].name+"/"+img+"'></div>");
				}
				$("#bossLists").html(lists.join(""));
				this.setScroll(n);
			},
			
			setScroll:function(m){
				
				var set;
				$('.recommend').sliderImg({
					pageWith:300,
					scrollWidth:900,
					leftMove:".left-button",
					rightMove:".right-button",
					onchange:function(t,n,settings){
						set = settings;
						if(n===null){
							return;
						}
						select(n);
					}
				});
						
				function select(n){
					console.log(n);
					var item = $(".recommend .items");
					for(var i=0; i<item.length; i++){
							item.eq(i).find(".item").removeClass("itemOn");
							item.eq(i).find(".item").eq(n).addClass("itemOn");
					}
					var boss_cost = $("#boss_cost");
					var boss_free = $("#boss_free");
					var boss_button = $("#boss_ok");
					boss_cost.html(item.eq(0).find(".item").eq(n).attr("cost"));
					boss_free.html(item.eq(0).find(".item").eq(n).attr("free"));
					boss_button.attr("roomId",item.eq(0).find(".item").eq(n).attr("roomId"));
				}
				move(m);
				select(m-1);
				function move(n){
					var width = 300;
					$(".recommend .items").css({
						"-webkit-transform": "translate("+(-(n+1)*width)+"px, 0px)"
				    }).show();
					set.data._trans = -(n+1)*width;
					select(n);
				}
			}
			
			
			
			
	}
	

		
		
	var createRole = {
			init:function(){
				var _t = this;
				var url = Game.getBaseURL() + "/role/list";
				$.ajax({
					url: url,
					dataType:"json",
					success: function(data){
						if(data.success){
							 _t.createRoleList(data.data.roles);
						}else{
							$().alert({content:data.message});
						}
					}
				});
			},
			
			createRoleList:function(data){
				var html=[];
				var nameArr=["唐 僧","悟 空","八 戒","沙 僧"];
				for(var i=0; i<data.length; i++){
					html.push("<li class='role_"+(i+1)+"' roleName = '"+data[i].roleResrouceName+"' roleId='"+data[i].id+"'>"+nameArr[i]+"</li>")
				}
				$("#roleList").html(html.join(""));
			}
			
	}
	
	
	var game = {
			list:function(){
				var _t = this;
				var url = Game.getBaseURL() + "/room/competitionList";
				$.ajax({
					url: url,
					dataType:"json",
					success: function(data){
						if(data){
							 _t.createGameList(data);
						}
					}
				});
			},
			
			/*创建比赛列表*/
			createGameList:function(data){
				var html = [];
				for(var i=data.length-1; i>=0; i--){
					if(data[i].name === "高级比赛场"){
						var textClass = "text-gjbsc";
					}else if(data[i].name === "中级比赛场"){
						var textClass = "text-zjbsc";
					}else if(data[i].name === "初级比赛场"){
						var textClass = "text-cjbsc";
					}
					
					if(data[i].isCanSignUp){
						var button = "<span class='text-bm num_icon'></span>";
					}else{
						var button = "<span class='text-bjb num_icon'></span>";
					}
					
					html.push("<li>"+
						"<span class='"+textClass+" num_icon'></span>"+
						"<p>进入限制"+data[i].beanFunLimitDesc+"</p>"+
						"<span roomId='"+data[i].roomId+"' allNum = '"+data[i].signUpNumber+"'  class='gameButton pokeyButton pokeyButton-1 button-blue-1 icon'>"+button+"</span>"+
					"</li>");
				}
				$("#gameLists").html(html.join());
				
				/*事件绑定*/
				var _t = this;
				$(".gameButton").click(function(){
					var roomId = $(this).attr("roomId");
					var allNum = $(this).attr("allNum");
					console.log(roomId);
					_t.signUp(roomId);
					_t._allNum = allNum;
					_t._roomId = roomId;
				})
			},
			
			/*报名*/
			signUp:function(roomId){
				var _t = this;
				var url = Game.getBaseURL() + "/room/signUp?roomId="+roomId;
				$.ajax({
					url: url,
					dataType:"json",
					success: function(data){
						_t.gameStat(2);
						/*现在的人数*/
						_t.gameAllNums();
						_t.gameNowNums(5);
						if(data.success){
							
						}else{
							$().alert({
								content : data.message
							});
						}
					}
				});
				
			},
			
			gameStat:function(n){
				if(n === 1){
					$(".gameRoom").show();
					$(".gameWait").hide();
					$(".gameAccounts").hide();
				}else if(n === 2){
					$(".gameRoom").hide();
					$(".gameWait").show();
					$(".gameAccounts").hide();
				}else if(n === 3){
					$(".gameRoom").hide();
					$(".gameWait").hide();
					$(".gameAccounts").show();
				}
			},
			
			gameNowNums:function(num){
				$("#nowNums").html(num);
				var allLength = $("#loadBar").width();
				var now = allLength/this._allNum*num;
				$("#loadBaring").width(now);
			},
			
			gameAllNums:function(){
				$("#allNums").html(this._allNum);
			}
	}
	
	
	/*商城*/
	/*
	// store/goods
	// store/buy/{goodsId}
	 */
	 
	var Mall = {
			goods:function(){
				var _t = this;
				var url = Game.getBaseURL() + "/store/goods";
				$.ajax({
					url: url,
					dataType:"json",
					success: function(data){
						if(data.success){
							_t.createGoods(data.data.goodsList);
						}else{
							$().alert({"content":data.message});
						}
						
					}
				});
			},
			
			createGoods:function(data){
				console.log(data);
				var goods = [];
				var _t = this;
				for(var i=0; i<data.length; i++){
					
					goods.push("<tr class='table_line'>"+
							    "<td style='width:15%'><span class='props_icon "+data[i].token+"'></span></td>"+
							    "<td style='width:20%'>"+data[i].name+"</td>"+
							    "<td style='width:35%'>"+data[i].desc+"</td>"+
							    "<td style='width:15%'>"+data[i].price+"</td>"+
							    "<td style='width:15%'><input type='button' goodsId='"+data[i].id+"'  class='btn_buy' /></td>"+
							  "</tr>");
				}
				
				$("#goodsLists").html(goods.join(""));
				$(".btn_buy").click(function(){
					var goodsId = $(this).attr("goodsId");
					$().alert({content:"购买成功！"})
					//_t.aJaxGoods(goodsId);
				})
			},
			
			aJaxGoods:function(goodsId){
				window.location.href = Game.getBaseURL() + "/store/buy/"+goodsId;
			}
			
			
	}
	
	
	var isOpenNewRoom={
			init:function(){
				var _t = this;
				var uid = dataStorage.get(MyOwner.userId);
				var url = Game.getBaseURL() + "/popup/opening?userId="+uid;
				$.ajax({
					url: url,
					dataType:"json",
					success: function(data){
						_t._data(data.data);
					}
				});
			},
			
			reslut:function(){
				var _t = this;
				var uid = dataStorage.get(MyOwner.userId);
				var url = Game.getBaseURL() + "/popup/opening?userId="+uid;
				$.ajax({
					url: url,
					dataType:"json",
					success: function(data){
						_t.reslutData(data.data);
					}
				});
			},
			
			_data:function(data){
				//var queue = new Queue();
				/*
				if(data.normalRoom){
					//queue.dealSyn(function(){
						require(["js/mEffect.js","js/mBox.js"], function(effect,box){ 
							box.toBoss("开启普通场");
						});
					//});
				}
				*/
				if(data.bossRoom){
					$("#room_boss").find("span").addClass("icon_gantan jumpAnmi");
				}else{
					$("#room_boss").find("span").removeClass("icon_gantan jumpAnmi");
				}
				
				if(data.competitionRoom){
					$("#room_bs").find("span").addClass("icon_gantan jumpAnmi");
				}else{
					$("#room_bs").find("span").removeClass("icon_gantan jumpAnmi");
				}
				
				if(data.beanfunRoom){
					$("#room_ld").find("span").addClass("icon_gantan jumpAnmi");
				}else{
					$("#room_ld").find("span").removeClass("icon_gantan jumpAnmi");
				}
				
			},
			
			reslutData:function(data){
				
				if(data.bossRoom){
					require(["js/mBox.js"], function(box){ 
						box.toBoss("开启Boss房间");
					});
				}
				
				if(data.competitionRoom){
					require(["js/mBox.js"], function(box){ 
						box.toCompetition("开启比赛场!");
					});
				}
				
				if(data.beanfunRoom){
					require(["js/mBox.js"], function(box){ 
						box.toBeanfun("开启乐豆场!");
					});
				}
				
			}
	}
	
	
	
	
	
	return {
		role:function(){
			//video.bg();
			//role/reap：参数 => userId, roleId
			//role/isCreapTime
			
			var uid = dataStorage.get(MyOwner.userId);
			
			R.setLists(uid,true);
			R.createUser(uid);
			
			$("#role_return").live(common.getEvent[0],function(){
	    		$(this).addClass("on");
	    	});
			$("#role_return").live(common.getEvent[1],function(){
	    		$(this).removeClass("on");
	    		R.change($("#roleId").val());
	    		setTimeout(function(){
	    			window.location.href = Game.getBaseURL()+"/index.jsp";
	    		},1000)
	    		
	    		
	    	});
			
			var niceButton = $("#niceButton.peopleHas-button");
			niceButton.live(common.getEvent[0],function(){
		    	$(this).addClass("on-120");
		    });
		    niceButton.live(common.getEvent[1],function(){
		    	$(this).removeClass("on-120");
		    	R.upgrade(uid);
		    	//role/upgrade：参数=> userId, resId（角色资源ID）, nextLevel
		    });
		},
		
		changeRole:function(n){
			var uid = dataStorage.get(MyOwner.userId);
			R.upgradeList(rolesArr[n],uid);
		},
		
		unlockBoss:function(){
			R.unlock();
		},
		
		create:function(){
			createRole.init();
			
			$("#login").live(common.getEvent[0],function(){
	    		$(this).addClass("on-120");
	    	});
			$("#login").live(common.getEvent[1],function(){
	    		$(this).removeClass("on-120");
	    		var name = $("#name").val();
				var roleName = $(this).attr("roleName");
				var roleId = $(this).attr("roleId");
				dataStorage.set(MyOwner.roleName,roleName);
				var url = Game.getBaseURL() + "/role/create?n="+name+"&r="+roleId;
				$.ajax({
					url: url,
					dataType:"json",
						success: function(data){
							if(data.success){
								location.href =  Game.getBaseURL() +"/index.jsp";
							}else{
								$().alert({"content":data.message});
							}
						}
					});
	    	});
			
		},
		
		
		room:function(){
			video.bg();
			room._createPlayerBox();
			$(".room").show();
			
			onRoomShow();
			connectToServer();
			
			
			
			var roomList = $("#startGame");
			roomList.live("click",function(){
		  		var room = $(this).attr("room");
		  		enterRoom(room);
			});
		},
		
		isOpenRoom:function(){
			isOpenNewRoom.init();
		},
		
		reslutOpenRoom:function(){
			isOpenNewRoom.reslut();
		},
		
		
		start:function(){
			
			
		  	
		  	var button = $("#start");
		  
		  	button.live(common.getEvent[0],function(){
	    		$(this).addClass("on-120");
	    		
	    	});
		  	button.live(common.getEvent[1],function(){
	    		$(this).removeClass("on-120");	
	    		
	    		var forTime=(new Date()).getTime();
	    		var createURl = Game.getBaseURL() + "/role/isCreate";
	    		$.ajax({
						url: createURl,
						dataType:"json",
						success: function(data){
							  	if(data.data.result){
										location.href = Game.getBaseURL() + "/index.jsp?"+forTime;
								}else{
										location.href = Game.getBaseURL() + "/create-new.jsp?"+forTime;
								}
						}
				});
	    	});
		  	
		  	
		  	var server = $("#server");
		  	server.live(common.getEvent[0],function(){
	    		$(this).addClass("on-120");
	    	});
		  	server.live(common.getEvent[1],function(){
	    		$(this).removeClass("on-120");
		  	});
		  	
		  	var account = $("#account");
		  	account.live(common.getEvent[0],function(){
	    		$(this).addClass("on-120");
	    	});
		  	account.live(common.getEvent[1],function(){
	    		$(this).removeClass("on-120");
		  	});
		  	
		  
		},
		
		boss:function(n){
			room._createPlayerBox();
			$("#room").show();
		
			connectToServer();
			
			Boss.init(n);
			
			var boss_ok = $("#boss_ok");
			var boss_return = $("#boss_return");
			boss_ok.live(common.getEvent[0],function(){
	    		$(this).addClass("on");
	    	});
			boss_ok.live(common.getEvent[1],function(){
	    		$(this).removeClass("on");
	    		var roomId = $(this).attr("roomId");
	    		console.log("Boss房间ID："+roomId);
	    		enterRoom(roomId);
	    	});
			
			boss_return.live(common.getEvent[0],function(){
	    		$(this).addClass("on");
	    	});
			boss_return.live(common.getEvent[1],function(){
	    		$(this).removeClass("on");
	    		window.history.go(-1);
	    	});
		},
		
		
		help:function(){
			var boss_return = $("#boss_return");
			
			boss_return.live(common.getEvent[0],function(){
	    		$(this).addClass("on");
	    	});
			boss_return.live(common.getEvent[1],function(){
	    		$(this).removeClass("on");
	    		window.history.go(-1);
	    	});
		},
		
		info:function(){
			
			var uid = dataStorage.get(MyOwner.userId);
			
			Info.setLists(uid,false);
			
			
			var boss_return = $("#boss_return");
			
			boss_return.live(common.getEvent[0],function(){
	    		$(this).addClass("on");
	    	});
			boss_return.live(common.getEvent[1],function(){
	    		$(this).removeClass("on");
	    		window.location.href = Game.getBaseURL()+"/index.jsp";
	    	});
		},
		
		game:function(){
			game.list();
			
			var boss_return = $("#boss_return");
			boss_return.live(common.getEvent[0],function(){
	    		$(this).addClass("on");
	    	});
			boss_return.live(common.getEvent[1],function(){
	    		$(this).removeClass("on");
	    		window.history.go(-1);
	    	});
		},
		
		gameStat:function(n){
			game.gameStat(n);
		},
		
		gameNowNums:function(num){
			game.gameNowNums(num);
		},
		
		gameStart:function(){
			enterRoom(game._roomId);
		},
		
		mall:function(){
			Mall.goods();
			
			var boss_return = $("#boss_return");
			boss_return.live(common.getEvent[0],function(){
	    		$(this).addClass("on");
	    	});
			boss_return.live(common.getEvent[1],function(){
	    		$(this).removeClass("on");
	    		window.history.go(-1);
	    	});
		},
		
		beanfun:function(){
			var rule = $("#beanfunRule");
			var boss_return = $("#return");
			var room =$("#beanfunRoom");
			
			
			
			boss_return.live(common.getEvent[0],function(){
	    		$(this).addClass("on");
	    	});
			boss_return.live(common.getEvent[1],function(){
	    		$(this).removeClass("on");
	    		window.history.go(-1);
	    	});
			
			rule.live("click",function(){
				require(["js/mBox.js"], function(box){ 
					box.beanfunRule();
				});
			});
			
			room.live("click",function(){
				
				$(".room").show();
				onRoomShow();
				connectToServer();
				enterRoom(55);
			})
		}
	}
}); 